package com.szh.es.testcase;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

import static com.szh.es.MyContant.MAPPING_TEMPLATE;

/**
 * 这里时测试，开发时：通过 ESTemplate操作。Spring进行了封装
 */
@Slf4j
public class ESIndexTestCase {
    public static void main(String[] args) throws IOException {

        // 创建客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200)));

        // 创建索引
        CreateIndexRequest indexRequest = new CreateIndexRequest("hotel");
        // 就是DSl 语句。可以是常量字符串
        indexRequest.source(MAPPING_TEMPLATE, XContentType.JSON);
        CreateIndexResponse indexResponse = esClient.indices().create(indexRequest, RequestOptions.DEFAULT);
        boolean acknowledged = indexResponse.isAcknowledged();
        log.error("响应{}",acknowledged);

        // 查询索引
        // GetIndexRequest getIndexRequest = new GetIndexRequest("book");
        // GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
        // log.info("getAliases:{}",getIndexResponse.getAliases());
        // log.info("getMappings:{}",getIndexResponse.getMappings());
        // log.info("getSettings:{}",getIndexResponse.getSettings());

        // 删除索引
        // AcknowledgedResponse deleteRes = esClient.indices().delete(new DeleteIndexRequest("book"), RequestOptions.DEFAULT);
        // boolean delAck = deleteRes.isAcknowledged();
        // log.error("delAck:{}",delAck);
        esClient.close();
    }

}
